package com.crm.controller.reportsController;

import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.crm.dao.AttachmentHistoryDAO;
import com.crm.dao.OpportunityDAO;
import com.crm.dao.PendingQuotationsDAO;
import com.crm.dao.QuotationDAO;
import com.crm.entity.AttachmentHistory;
import com.crm.entity.Business;
import com.crm.entity.Opportunity;
import com.crm.entity.Quotation;
import com.crm.util.Constants;

@Controller
@RequestMapping("/pendingQuot")
public class PendingQuotController {
	protected final Log logger = LogFactory.getLog(getClass());
	@RequestMapping("/show.do")
	public ModelAndView show(ModelAndView model,HttpSession session,HttpServletRequest request,HttpServletResponse Response){
		Business bus=(Business)session.getAttribute("business");
		if(bus==null)return new ModelAndView("/jsp/index.jsp");
		int busid=bus.getBusinessId();
		//OpportunityDAO odao=new OpportunityDAO();
		String status=Constants.ST_GENERATED_QUOTATION;
		List<Opportunity>  lop=null;
		try {
			lop=OpportunityDAO.getSingleInstance().getByStatus(status, busid);
		} catch(NullPointerException npe){
			logger.info("Null Pointer Exception Occured");
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return model=new ModelAndView("/jsp/pending-quotations.jsp")
		.addObject("submenu", "reports")
		.addObject("menuName","reports")
		.addObject("opplist",lop);
	}
	
	
	
	@RequestMapping("/closeQuot.do")
	public ModelAndView closeQuot(ModelAndView model,HttpSession session,HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Business bus=(Business)session.getAttribute("business");
		String refno=request.getParameter("refid");
		int refid=Integer.parseInt(refno);
		QuotationDAO quotDAO=new QuotationDAO();
		Quotation quot=quotDAO.getPendingQuot(refid,bus.getBusinessId());
		return model=new ModelAndView("/jsp/closeQuot.jsp")
		.addObject("submenu", "reports")
		.addObject("menuName","reports")
		.addObject("quotobj",quot);
		
	}
	
	@RequestMapping(value="/get_history.do",method=RequestMethod.GET)
	public String htmlReport(HttpSession session,HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Business bus=(Business)session.getAttribute("business");
		String refno=request.getParameter("refid");
		int refid=Integer.parseInt(refno);
		QuotationDAO quotDAO=new QuotationDAO();
		Quotation quot=quotDAO.getPendingQuot(refid,bus.getBusinessId());
		String quotno=quot.getQuotno();
		AttachmentHistoryDAO atDAO=new AttachmentHistoryDAO();
		AttachmentHistory att=atDAO.getAttachmentHistoryByRefno(bus.getBusinessId(),"QUOTATION",quotno.trim());
		logger.info("the quotation no is "+quotno.trim());
		logger.info("the business id is  "+bus.getBusinessId());
		if(att==null){
			System.out.println("This enquiry does not have quotation");
			return null;
			
		}
		byte[] pdfReport =att.getFileBlob();
		logger.info("the length of pdf is"+pdfReport.length);
		logger.info("the length of pdf is"+pdfReport.toString());
		
		ServletOutputStream servletOutputStream = response.getOutputStream();
		response.setContentType(att.getFileType());
		response.setContentLength(pdfReport.length);
		servletOutputStream.write(pdfReport);
		servletOutputStream.flush();
		servletOutputStream.close(); 
		return null;
	   
	}
	
	
	
	
	@RequestMapping("/update.do")
	public ModelAndView update(ModelAndView model,HttpSession session,HttpServletRequest request,HttpServletResponse Response) throws Exception{
		Business bus=(Business)session.getAttribute("business");
		if(bus==null)return new ModelAndView("/jsp/index.jsp");
		int busid=bus.getBusinessId();
		PendingQuotationsDAO pqDAO=new PendingQuotationsDAO();
		String srowCount=request.getParameter("rowCount");
		int rowCount=Integer.parseInt(srowCount);
		for(int i=0;i<rowCount;i++){
			String sid=request.getParameter("id"+(i+1));
			String remarks=request.getParameter("remarks"+(i+1));
			int id=Integer.parseInt(sid);
			pqDAO.updateRemarks(id, remarks, busid);
		}
		return model=new ModelAndView("/jsp/pending-quotations.jsp")
		.addObject("submenu", "reports")
		.addObject("menuName","reports")
		.addObject("msg","Successfully Updated");
	}
}
